package com.zdp.algorithm.stackandqueue;

import lombok.Data;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 猫狗队列
 * @author Zhengdp
 * @date 2023年01月05日 14:15
 */
public class CatDogQueue {
}

class CatDogQueueImpl {
    private Queue<Entity> dogQueue;
    private Queue<Entity> catQueue;
    private Long num ;
    public CatDogQueueImpl() {
        this.dogQueue = new LinkedList<>();
        this.catQueue = new LinkedList<>();
        this.num = 0L;
    }

    public void add(Pet pet) {
        if("Dog".equals(pet.getType())) {
            dogQueue.add(new Entity(pet,num++));
        }else {
            catQueue.add(new Entity(pet,num++));
        }
    }

    public Pet pollAll(){
        if(dogQueue.isEmpty() && catQueue.isEmpty()) {
            throw new RuntimeException("当前队列为空");
        }
        if(dogQueue.isEmpty()) {
            return catQueue.poll().getPet();
        }else if(catQueue.isEmpty()) {
            return dogQueue.poll().getPet();
        }else{
            Entity dog = dogQueue.peek();
            Entity cat = catQueue.peek();
            return dog.getNum() > cat.getNum()
                    ? catQueue.poll().getPet() : dogQueue.poll().getPet();
        }
    }

    public Pet pollDog(){
        if(dogQueue.isEmpty()) {
            throw new RuntimeException("当前队列中没有Dog");
        }
        return dogQueue.poll().getPet();
    }

    public Pet pollCat(){
        if(catQueue.isEmpty()) {
            throw new RuntimeException("当前队列中没有Cat");
        }
        return catQueue.poll().getPet();
    }
}

@Data
class Entity {
    private Pet pet;
    // 用于区分进入队列的顺序
    private Long num;
    public Entity(Pet pet , Long num) {
        this.pet = pet;
        this.num = num;
    }
}

class Dog extends Pet {
    public Dog() {
        super("Dog");
    }
}

class Cat extends Pet {
    public Cat() {
        super("Cat");
    }
}
class Pet {
    String type ;
    public Pet(String type) {
        this.type = type;
    }

    public String getType(){
        return this.type;
    }
}
